$(function () {
   //用户信息
   {
      $.get('/api/me', res => {

         $('#user_profile')[0].src = '/profile/' + res.profile
      })
      $.get('/api/info', res => {
         const info_input = $('.Fatal-Frame')
         info_input.eq(0).text(res.info.read)
         info_input.eq(2).text(res.info.fans)
         info_input.eq(3).text(res.info.collection)
         const classif_ul = $('#classif_ul')
         if (res.classif) {
            res.classif.forEach(a => {
               classif_ul.append(`
               <li class="el-select-dropdown__item">
                  ${a.name}
               </li>`)
            })
         }
      })
   }
   // 展开页
   $('.stretch-box .box').on('click', function () {
      const el = $(this).siblings('.submenu')
      if ($(this).data('stretch')) {
         $(this).data('stretch', false)
         $(this).removeClass('disi')
         el.css('height', '0px')
      } else {
         $(this).data('stretch', true)
         $(this).addClass('disi')
         el.css('height', el.find('dd').length * 40)
      }
   })
   // 切换页
   {
      $('.index_nav').click(function () {
         $('.console_home_box').show().siblings().hide()
      })
      let bl
      $('.main_manage_nav').click(function () {
         if (!bl) {
            bl = true
            screen_ajax()
         }
         $('.content_management_box').show().siblings().hide()
      })
   }
   // 首页
   {
      $('.console_home_block_box .el-menu-item').on('click', function () {
         const t = $(this).index()
         $(this).addClass('active').siblings().removeClass('active')
         $('.console-home-body .console-home-body-list').eq(t).addClass('dwe').siblings().removeClass('dwe')
      })
      $.get('/api/article/nearpush', res => {
         if (res.code == 200) {
            if(res.article.length) {
               const box = $('.console-home-body-list.dwe')
               const html = res.article.map(a => `
                  <div class="option" data-id="${a.id}">
                     <div class="type">
                        <span class="bk">
                        </span>
                     </div>
                     <a target="_blank" href="/article/${a.id}" class="title_abstract">
                        <div class="title">
                           ${a.title}
                        </div>
                        <div class="abstract">
                           ${a.abstract}
                        </div>
                     </a>
                     <div class="read info">
                        <div class="text">
                           ${a.read}
                        </div>
                        <div class="dps">
                           阅读量
                        </div>
                     </div>
                     <div class="stretch info">
                        <div class="text">
                           0
                        </div>
                        <div class="dps">
                           评论数
                        </div>
                     </div>
                     <div class="agree info">
                        <div class="text">
                           ${a.agree}
                        </div>
                        <div class="dps">
                           点赞数
                        </div>
                     </div>
                     <div class="collection info">
                        <div class="text">
                           ${a.collection}
                        </div>
                        <div class="dps">
                           收藏数
                        </div>
                     </div>
                  </div>`);
               box.html(html.join(''))
            }
         }
      })
   }
   // 文章管理
   {
      // 筛选
      $('.el-select .el-input input').on({
         focus() {
            $(this).parents('.el-select').addClass('open').siblings().removeClass('open')
         },
      })
      $('.el-select').on('click', '.el-select-dropdown__item', function () {
         $(this).parents('.el-select').find('.el-input input').val($(this).text().trim())
      })
      $(document).on('click', event => {
         const el = $('.el-select.open')
         if (el.length) {
            if (!$(event.target).hasClass('el-input__inner')) {
               el.removeClass('open')
            }
         }
      })
      $('.el-select .el-input').on('click', function (event) {
         $(this).find('.el-input__inner').focus()
         event.stopPropagation()
      })

      // 筛选
      function screen_ajax() {
         const data = $('.view-container-blog').serializeArray()
         const query = {}
         data.forEach(a => {
            const { name, value } = a
            if (value) {
               if (name === 'state') {
                  switch (value) {
                     case '0':
                        query.so = 0
                        break;
                     case '1':
                        query.so = 1
                        break
                     case '2':
                        query.state = 0
                        break
                     case '3':
                        query.del = 1
                        break
                  }
               } else if (name == 'screen') {
                  query.screen = value
               } else if (value == '不限') {
                  return
               } else if (name == 'type') {
                  switch (value) {
                     case '原创':
                        query.type = '0'
                        break;
                     case '转载':
                        query.type = '1'
                        break;
                     case '翻译':
                        query.type = '2'
                        break;
                  }
               } else {
                  query[name] = value
               }
            }
         })
         const blank = $('.content_management_box .home-vipserver-box-blank')
         const ul = $('.article_ul')
         $.get('/api/article/list', query, res => {
            if (res.code == 200 && res.article.length) {
               blank.hide()
               const html = res.article.map(a => `
               <li class="article_li">
                  ${a.cover ? a.cover.split(' ').map(b => `<a target="_blank" href="/editor?id=${a.id}"><img class="cover" src="/userupimage/${b}" alt=""></a>`) : ''}
                  <div class="info">
                     <div class="top">
                        <a target="_blank" href="/editor?id=${a.id}" class="title">${a.title}</a>
                        <div class="date">${(new Date(a.createdAt)).toLocaleString()}</div>
                     </div>
                     <div class="tag_box">
                     ${a.state == 0 ? `<span class="tag state_0"></span>` : ''}
                     ${a.type !== null ? `<span class="tag type_${a.type}"></span>` : ''}
                     ${a.state ? `<span class="tag state_1"></span>` : ''}
                     </div>
                     <div class="bom">
                        <div class="info_num"> 阅读 <span>${a.read}</span> · 评论 <span>0</span> · 收藏 <span>${a.collection}</span>
                           <span></span>
                        </div>
                        <div class="operation">
                           <a target="_blank" href="/editor?id=${a.id}">编辑</a>
                           <a target="_blank" href="article/${a.id}">浏览</a>
                           <a class="delete_btn" data-id="${a.id}" href="javascript:;  ">删除</a>
                        </div>
                     </div>
                  </div>
               </li>
               `)
               ul.html(html.join(''))
            } else {
               blank.show()
               ul.html('')
            }
         })
      }
      $('#pills-tab .nav-item input').change(function () {
         $(this).parents('.nav-item').addClass('active').siblings().removeClass('active')
         screen_ajax()
      })
      $('.view-container-blog input').on('keydown', event => {
         if (event.key == 'Enter') {
            screen_ajax()
         }
      })
      $('.main-search-searchButton').on('click', function () {
         screen_ajax()
      })
      $('.article_ul').on('click', '.delete_btn', function() {
         const id = $(this).data('id')
         $.get('/api/article/delete', {id}, res => {
            if(res.code == 200) {
               $(this).parents('.article_li').remove()
               alterBlock('删除成功')
            } else {
               alterBlock('删除失败')
            }
         })
      })
   }
})